package com.xujianjie.userservice.controller;

import com.xujianjie.userservice.model.ResponseData;
import com.xujianjie.userservice.model.User;
import com.xujianjie.userservice.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResponseData info(Integer userId)
    {
        User user = userService.findById(userId);
        if (user != null)
        {
            return new ResponseData(ResponseData.STATUS_OK, user, "获取成功！");
        }
        else
        {
            return new ResponseData(ResponseData.STATUS_FAILED, null, "获取失败！");
        }
    }

    @RequestMapping(value = "/balance/deduct", method = RequestMethod.GET)
    public ResponseData deductBalance(Integer userId, Integer money)
    {
        if (userService.deductBalance(userId, money))
        {
            return new ResponseData(ResponseData.STATUS_OK, null, "扣减成功！");
        }
        else
        {
            return new ResponseData(ResponseData.STATUS_FAILED, null, "扣减失败！");
        }
    }
}
